<template>
  <div class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul v-if="goods" class="menu">
        <li v-for="(item,index) in goods" class="item-foods" :class="currentIndex === index ? activeClass:''"
            @click="selectItem(index,$event)">
          <div class="line">
             <span class="text"><span v-if="item.type > 0" class="icon"
                                      :class="classMap[item.type]"></span>{{item.name}}</span>
          </div>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul v-if="goods">
        <li v-for="item in goods" class="item-foods-hook">
          <h1 class="title"><span>{{item.name}}</span></h1>
          <ul v-if="item.foods">
            <li v-for="foodsItem in item.foods" class="item-foods">
              <div class="icon"><img width="48" height="48" :src="foodsItem.icon"/></div>
              <div class="content">
                <div class="name">{{foodsItem.name}}</div>
                <div class="des">{{foodsItem.description}}</div>
                <div class="num"><span>月售{{foodsItem.sellCount}}，</span><span>好评率{{foodsItem.rating}}%</span></div>
                <div class="price">￥{{foodsItem.price}}<span v-if="foodsItem.oldPrice != ''? true : false"
                                                             class="old-price">￥{{foodsItem.oldPrice}}</span></div>
              </div>
              <div class="cartcontrol-wrapper">
                <cartcontrol :food="foodsItem"></cartcontrol>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shopcart :delivery-price="seller.deliveryPrice" :min-price="seller.minPrice"
              :select-foods="selectFoods"></shopcart>
  </div>
</template>

<script type="text/ecmascript-6">

  import BetterScroll from 'better-scroll';
  import shopcart from 'components/shopcart/shopcart';
  import cartcontrol from 'components/cartcontrol/cartcontrol';

  let ok = 0;

  export default{
    props: {seller: Object},
    data: function () {
      return {
        activeClass: 'current',
        goods: [],
        listHeight: [],
        scrollY: 0
      };
    },
    created: function () {
      this.$http.get('/api/goods').then(response => {
        response = response.body;

        if (response.errno === ok) {
          this.goods = response.data;
          this.$nextTick(function () {
            this.initScroll();
            this.calculateHeight();
          });
        }
      });

      this.$root.eventHub.$on('cartAdd', (target) => {
        this.drop(target);
      });

      this.classMap = ['descrease', 'discount', 'special', 'invoice', 'guarantee'];
    },
    computed: {
      currentIndex: function () {
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i];
          let height2 = this.listHeight[i + 1];

          if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            return i;
          }
        }
        return 0;
      },
      selectFoods: function () {
        let foods = [];
        this.goods.forEach((good) => {
          good.foods.forEach((food) => {
            if (food.count) {
              foods.push(food);
            }
          });
        });
        return foods;
      }
    },
    methods: {
      initScroll: function () {
        this.menuScroll = new BetterScroll(this.$refs.menuWrapper, {
          click: true
        });
        this.foodsScroll = new BetterScroll(this.$refs.foodsWrapper, {
          probeType: 3,
          click: true
        });

        this.foodsScroll.on('scroll', (pos) => {
          this.scrollY = Math.abs(Math.round(pos.y));
        });
      },
      calculateHeight: function () {
        let foodsItem = this.$refs.foodsWrapper.getElementsByClassName('item-foods-hook');
        let height = 0;
        this.listHeight.push(height);
        for (let i = 0; i < foodsItem.length; i++) {
          height += foodsItem[i].clientHeight;
          this.listHeight.push(height);
        }
      },
      selectItem: function (index, event) {
        if (!event._constructed) {
          return;
        }
        let el = this.$refs.foodsWrapper.getElementsByClassName('item-foods-hook')[index];
        this.foodsScroll.scrollToElement(el, 200);
      }
    },
    components: {
      shopcart,
      cartcontrol
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">

  @import "../../common/stylus/mixin.styl";

  .goods
    display: flex
    position: absolute
    top: 174px
    bottom: 46px
    width: 100%
    overflow: hidden
    .menu-wrapper
      flex: 0 0 80px
      width: 80px
      background: #f3f5f7
      .item-foods
        width: 100%
        height: 54px
        &.current
          background: #fff
          .line
            margin-top: -1px;
            border-top: 1px solid #fff
            border-none()
          .text
            font-weight: 700
        &:last-child
          border-none()
        .line
          display: table
          width: 75%
          height: 54px
          font-size: 0
          margin: 0 10px
          border-bottom-1px(#d9dde1)
        .icon
          display: inline-block
          width: 10px
          height: 10px
          margin-right: 2px
          background-size: 10px 10px
          background-repeat: no-repeat
          vertical-align: top
          &.descrease
            bg-image('decrease_3')
          &.discount
            bg-image('discount_3')
          &.guarantee
            bg-image('guarantee_3')
          &.invoice
            bg-image('invoice_3')
          &.special
            bg-image('special_3')
        .text
          display: table-cell
          vertical-align: middle
          font-size: 12px
          color: rgb(77, 85, 93)
          font-weight: 200
          line-height: 14px

    .foods-wrapper
      flex: 1
      .title
        height: 26px
        line-height: 26px
        background: #f3f5f7
        color: rgb(147, 153, 159)
        border-left: 2px solid #d9dde1
        font-size: 12px
        & > span
          margin-left: 14px
      .item-foods
        display: flex
        margin: 18px
        padding-bottom: 18px
        border-bottom-1px(rgba(7, 17, 27, 0.1))
        font-size: 0
        &:last-child
          margin-bottom: 0
          border-none()
        .icon
          flex: 0 0 48px
          display: inline-block
          vertical-align: top;
          margin-right: 10px
        .content
          flex: 1
          display: inline-block
          .name
            font-size: 14px
            color: rgb(7, 17, 27)
          .des, .num
            font-size: 10px
            color: rgb(147, 153, 159)
            margin: 8px 0
          .price
            font-size: 14px
            color: rgb(240, 20, 20)
          .old-price
            margin-left: 8px
            font-size: 10px
            color: rgb(147, 153, 159)
            font-weight: 700
            text-decoration: line-through

        .cartcontrol-wrapper
          position: absolute
          right: -6px
          bottom: 12px
</style>
